package org.rowan.infovis.soccer;

import com.sun.opengl.util.GLUT;
import javax.media.opengl.GL;

public class CategoryNav {

    GLUT glut = new GLUT();
    private float HEIGHT = 347f;
    private float WIDTH = 115f;
    private float PAD = 3f;

    private float LINK_WIDTH = 110f;
    private float LINK_HEIGHT = 40f;

    private float KICK_X = PAD;
    private float KICK_Y = HEIGHT - LINK_HEIGHT - PAD;

    private boolean onKick = true;
    boolean kickSelected = true;

    private float HEADBALL_X = PAD;
    private float HEADBALL_Y = KICK_Y - LINK_HEIGHT - PAD;

    private boolean onHeadball = false;
    boolean headballSelected = false;

    private float THROWIN_X = PAD;
    private float THROWIN_Y = HEADBALL_Y - LINK_HEIGHT - PAD;

    private boolean onThrowin = false;
    boolean throwinSelected = false;

    private float TRAP_X = PAD;
    private float TRAP_Y = THROWIN_Y - LINK_HEIGHT - PAD;
    private boolean onTrap = false;
    boolean trapSelected = false;

    private float TACKLING_X = PAD;
    private float TACKLING_Y = TRAP_Y - LINK_HEIGHT - PAD;

    private boolean onTackling = false;
    boolean tacklingSelected = false;

    private float DRIBBLING_X = PAD;
    private float DRIBBLING_Y = TACKLING_Y - LINK_HEIGHT - PAD;

    private boolean onDribbling = false;
    boolean dribblingSelected = false;

    private float FOOTWORK_X = PAD;
    private float FOOTWORK_Y = DRIBBLING_Y - LINK_HEIGHT - PAD;

    private boolean onFootwork = false;
    boolean footworkSelected = false;

    private float PROTECTION_X = PAD;
    private float PROTECTION_Y = FOOTWORK_Y - LINK_HEIGHT - PAD;

    private boolean onProtection = false;
    boolean protectionSelected = false;

    private float startX;
    private float startY;

    public float getHeight() {
        return HEIGHT;
    }

    public float getWidth() {
        return WIDTH;
    }

    public void draw(GL gl, float x, float y) {

        startX = x;
        startY = y;

        drawBorder(gl);

        drawKick(gl);

        drawHeadball(gl);

        drawThrowin(gl);

        drawTrap(gl);

        drawTackling(gl);

        drawDribbling(gl);

        drawFootwork(gl);

        drawBallProtection(gl);
    }

    private void drawBorder(GL gl) {
        gl.glPushMatrix();
        gl.glColor3f(1f, 1f, 1f);
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(0f, 0f);
        gl.glVertex2f(0f, HEIGHT);
        gl.glVertex2f(WIDTH, HEIGHT);
        gl.glVertex2f(WIDTH, 0f);
        gl.glEnd();
        gl.glPopMatrix();
    }

    private void setLinkColor(GL gl) {
        gl.glColor3f(.8f, .8f, .8f);
    }

    private void setHoverLinkColor(GL gl) {
        gl.glColor3f(1f, 0f, 0f);
    }

    private void setSelectedLinkColor(GL gl) {
        gl.glColor3f(0f, 0, 1f);
    }

    private void drawKick(GL gl) {
        if (kickSelected) {
            setSelectedLinkColor(gl);
        } else if (onKick) {
            setHoverLinkColor(gl);
        } else {
            setLinkColor(gl);
        }

        gl.glPushMatrix();
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(KICK_X, KICK_Y);
        gl.glVertex2f(KICK_X, KICK_Y + LINK_HEIGHT);
        gl.glVertex2f(KICK_X + LINK_WIDTH, KICK_Y + LINK_HEIGHT);
        gl.glVertex2f(KICK_X + LINK_WIDTH, KICK_Y);
        gl.glEnd();
        gl.glPopMatrix();

        gl.glPushMatrix();
        gl.glColor3f(0f, 0f, 0f);
        gl.glRasterPos2f(KICK_X + 33f, KICK_Y + 15f);
        glut.glutBitmapString(GLUT.BITMAP_9_BY_15, "Kick");
        gl.glPopMatrix();
    }

    private void drawHeadball(GL gl) {
        if (headballSelected) {
            setSelectedLinkColor(gl);
        } else if (onHeadball) {
            setHoverLinkColor(gl);
        } else {
            setLinkColor(gl);
        }

        gl.glPushMatrix();
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(HEADBALL_X, HEADBALL_Y);
        gl.glVertex2f(HEADBALL_X, HEADBALL_Y + LINK_HEIGHT);
        gl.glVertex2f(HEADBALL_X + LINK_WIDTH, HEADBALL_Y + LINK_HEIGHT);
        gl.glVertex2f(HEADBALL_X + LINK_WIDTH, HEADBALL_Y);
        gl.glEnd();
        gl.glPopMatrix();

        gl.glPushMatrix();
        gl.glColor3f(0f, 0f, 0f);
        gl.glRasterPos2f(HEADBALL_X + 20f, HEADBALL_Y + 15f);
        glut.glutBitmapString(GLUT.BITMAP_9_BY_15, "Headball");
        gl.glPopMatrix();
    }

    private void drawThrowin(GL gl) {
        if (throwinSelected) {
            setSelectedLinkColor(gl);
        } else if (onThrowin) {
            setHoverLinkColor(gl);
        } else {
            setLinkColor(gl);
        }

        gl.glPushMatrix();
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(THROWIN_X, THROWIN_Y);
        gl.glVertex2f(THROWIN_X, THROWIN_Y + LINK_HEIGHT);
        gl.glVertex2f(THROWIN_X + LINK_WIDTH, THROWIN_Y + LINK_HEIGHT);
        gl.glVertex2f(THROWIN_X + LINK_WIDTH, THROWIN_Y);
        gl.glEnd();
        gl.glPopMatrix();

        gl.glPushMatrix();
        gl.glColor3f(0f, 0f, 0f);
        gl.glRasterPos2f(THROWIN_X + 20f, THROWIN_Y + 15f);
        glut.glutBitmapString(GLUT.BITMAP_9_BY_15, "Throw-In");
        gl.glPopMatrix();
    }

    private void drawTrap(GL gl) {
        if (trapSelected) {
            setSelectedLinkColor(gl);
        } else if (onTrap) {
            setHoverLinkColor(gl);
        } else {
            setLinkColor(gl);
        }

        gl.glPushMatrix();
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(TRAP_X, TRAP_Y);
        gl.glVertex2f(TRAP_X, TRAP_Y + LINK_HEIGHT);
        gl.glVertex2f(TRAP_X + LINK_WIDTH, TRAP_Y + LINK_HEIGHT);
        gl.glVertex2f(TRAP_X + LINK_WIDTH, TRAP_Y);
        gl.glEnd();
        gl.glPopMatrix();

        gl.glPushMatrix();
        gl.glColor3f(0f, 0f, 0f);
        gl.glRasterPos2f(TRAP_X + 33f, TRAP_Y + 15f);
        glut.glutBitmapString(GLUT.BITMAP_9_BY_15, "Trap");
        gl.glPopMatrix();
    }

    private void drawTackling(GL gl) {
        if (tacklingSelected) {
            setSelectedLinkColor(gl);
        } else if (onTackling) {
            setHoverLinkColor(gl);
        } else {
            setLinkColor(gl);
        }

        gl.glPushMatrix();
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(TACKLING_X, TACKLING_Y);
        gl.glVertex2f(TACKLING_X, TACKLING_Y + LINK_HEIGHT);
        gl.glVertex2f(TACKLING_X + LINK_WIDTH, TACKLING_Y + LINK_HEIGHT);
        gl.glVertex2f(TACKLING_X + LINK_WIDTH, TACKLING_Y);
        gl.glEnd();
        gl.glPopMatrix();

        gl.glPushMatrix();
        gl.glColor3f(0f, 0f, 0f);
        gl.glRasterPos2f(TACKLING_X + 20f, TACKLING_Y + 15f);
        glut.glutBitmapString(GLUT.BITMAP_9_BY_15, "Tackling");
        gl.glPopMatrix();
    }

    private void drawDribbling(GL gl) {
        if (dribblingSelected) {
            setSelectedLinkColor(gl);
        } else if (onDribbling) {
            setHoverLinkColor(gl);
        } else {
            setLinkColor(gl);
        }

        gl.glPushMatrix();
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(DRIBBLING_X, DRIBBLING_Y);
        gl.glVertex2f(DRIBBLING_X, DRIBBLING_Y + LINK_HEIGHT);
        gl.glVertex2f(DRIBBLING_X + LINK_WIDTH, DRIBBLING_Y + LINK_HEIGHT);
        gl.glVertex2f(DRIBBLING_X + LINK_WIDTH, DRIBBLING_Y);
        gl.glEnd();
        gl.glPopMatrix();

        gl.glPushMatrix();
        gl.glColor3f(0f, 0f, 0f);
        gl.glRasterPos2f(DRIBBLING_X + 15f, DRIBBLING_Y + 15f);
        glut.glutBitmapString(GLUT.BITMAP_9_BY_15, "Dribbling");
        gl.glPopMatrix();
    }

    private void drawFootwork(GL gl) {
        if (footworkSelected) {
            setSelectedLinkColor(gl);
        } else if (onFootwork) {
            setHoverLinkColor(gl);
        } else {
            setLinkColor(gl);
        }

        gl.glPushMatrix();
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(FOOTWORK_X, FOOTWORK_Y);
        gl.glVertex2f(FOOTWORK_X, FOOTWORK_Y + LINK_HEIGHT);
        gl.glVertex2f(FOOTWORK_X + LINK_WIDTH, FOOTWORK_Y + LINK_HEIGHT);
        gl.glVertex2f(FOOTWORK_X + LINK_WIDTH, FOOTWORK_Y);
        gl.glEnd();
        gl.glPopMatrix();

        gl.glPushMatrix();
        gl.glColor3f(0f, 0f, 0f);
        gl.glRasterPos2f(FOOTWORK_X + 20f, FOOTWORK_Y + 15f);
        glut.glutBitmapString(GLUT.BITMAP_9_BY_15, "Footwork");
        gl.glPopMatrix();
    }

    private void drawBallProtection(GL gl) {
        if (protectionSelected) {
            setSelectedLinkColor(gl);
        } else if (onProtection) {
            setHoverLinkColor(gl);
        } else {
            setLinkColor(gl);
        }

        gl.glPushMatrix();
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(PROTECTION_X, PROTECTION_Y);
        gl.glVertex2f(PROTECTION_X, PROTECTION_Y + LINK_HEIGHT);
        gl.glVertex2f(PROTECTION_X + LINK_WIDTH, PROTECTION_Y + LINK_HEIGHT);
        gl.glVertex2f(PROTECTION_X + LINK_WIDTH, PROTECTION_Y);
        gl.glEnd();
        gl.glPopMatrix();

        gl.glPushMatrix();
        gl.glColor3f(0f, 0f, 0f);
        gl.glRasterPos2f(PROTECTION_X + 35f, PROTECTION_Y + 25f);
        glut.glutBitmapString(GLUT.BITMAP_9_BY_15, "Ball");
        gl.glRasterPos2f(PROTECTION_X + 10f, PROTECTION_Y + 5f);
        glut.glutBitmapString(GLUT.BITMAP_9_BY_15, "Protection");
        gl.glPopMatrix();
    }

    private boolean insideKick(int x, int y) {
        if (x >= KICK_X && x <= (KICK_X + LINK_WIDTH) &&
                y >= KICK_Y && y <= (KICK_Y + LINK_HEIGHT)) {
            return true;
        }

        return false;
    }

    private boolean insideHeadball(int x, int y) {
        if (x >= HEADBALL_X && x <= (HEADBALL_X + LINK_WIDTH) &&
                y >= HEADBALL_Y && y <= (HEADBALL_Y + LINK_HEIGHT)) {
            return true;
        }

        return false;
    }

    private boolean insideThrowin(int x, int y) {
        if (x >= THROWIN_X && x <= (THROWIN_X + LINK_WIDTH) &&
                y >= THROWIN_Y && y <= (THROWIN_Y + LINK_HEIGHT)) {
            return true;
        }

        return false;
    }

    private boolean insideTrap(int x, int y) {
        if (x >= TRAP_X && x <= (TRAP_X + LINK_WIDTH) &&
                y >= TRAP_Y && y <= (TRAP_Y + LINK_HEIGHT)) {
            return true;
        }

        return false;
    }

    private boolean insideTackling(int x, int y) {
        if (x >= TACKLING_X && x <= (TACKLING_X + LINK_WIDTH) &&
                y >= TACKLING_Y && y <= (TACKLING_Y + LINK_HEIGHT)) {
            return true;
        }

        return false;
    }

    private boolean insideDribbling(int x, int y) {
        if (x >= DRIBBLING_X && x <= (DRIBBLING_X + LINK_WIDTH) &&
                y >= DRIBBLING_Y && y <= (DRIBBLING_Y + LINK_HEIGHT)) {
            return true;
        }

        return false;
    }

    private boolean insideFootwork(int x, int y) {
        if (x >= FOOTWORK_X && x <= (FOOTWORK_X + LINK_WIDTH) &&
                y >= FOOTWORK_Y && y <= (FOOTWORK_Y + LINK_HEIGHT)) {
            return true;
        }

        return false;
    }

    private boolean insideProtection(int x, int y) {
        if (x >= PROTECTION_X && x <= (PROTECTION_X + LINK_WIDTH) &&
                y >= PROTECTION_Y && y <= (PROTECTION_Y + LINK_HEIGHT)) {
            return true;
        }

        return false;
    }

    public void mouseMoved(int x, int y) {
        onKick = false;
        onHeadball = false;
        onThrowin = false;
        onTrap = false;
        onTackling = false;
        onDribbling = false;
        onFootwork = false;
        onProtection = false;

        if (x >= startX &&
                x <= (startX + WIDTH) &&
                y >= startY &&
                y <= (startY + HEIGHT)) {
            int mouseX = x - (int) startX;
            int mouseY = y - (int) startY;

            if (insideKick(mouseX, mouseY)) {
                onKick = true;
            } else if (insideHeadball(mouseX, mouseY)) {
                onHeadball = true;
            } else if (insideThrowin(mouseX, mouseY)) {
                onThrowin = true;
            } else if (insideTrap(mouseX, mouseY)) {
                onTrap = true;
            } else if (insideTackling(mouseX, mouseY)) {
                onTackling = true;
            } else if (insideDribbling(mouseX, mouseY)) {
                onDribbling = true;
            } else if (insideFootwork(mouseX, mouseY)) {
                onFootwork = true;
            } else if (insideProtection(mouseX, mouseY)) {
                onProtection = true;
            }
        }
    }

    public void mouseReleased(int x, int y) {

        if (x >= startX &&
                x <= (startX + WIDTH) &&
                y >= startY &&
                y <= (startY + HEIGHT)) {
            kickSelected = false;
            headballSelected = false;
            throwinSelected = false;
            trapSelected = false;
            tacklingSelected = false;
            dribblingSelected = false;
            footworkSelected = false;
            protectionSelected = false;

            int mouseX = x - (int) startX;
            int mouseY = y - (int) startY;

            if (insideKick(mouseX, mouseY)) {
                kickSelected = true;
            } else if (insideHeadball(mouseX, mouseY)) {
                headballSelected = true;
            } else if (insideThrowin(mouseX, mouseY)) {
                throwinSelected = true;
            } else if (insideTrap(mouseX, mouseY)) {
                trapSelected = true;
            } else if (insideTackling(mouseX, mouseY)) {
                tacklingSelected = true;
            } else if (insideDribbling(mouseX, mouseY)) {
                dribblingSelected = true;
            } else if (insideFootwork(mouseX, mouseY)) {
                footworkSelected = true;
            } else if (insideProtection(mouseX, mouseY)) {
                protectionSelected = true;
            }
        }
    }

    public int getMode() {
        if (kickSelected) {
            return 1;
        }
        if (headballSelected) {
            return 2;
        }
        if (throwinSelected) {
            return 3;
        }
        if (trapSelected) {
            return 4;
        }
        if (tacklingSelected) {
            return 5;
        }
        if (dribblingSelected) {
            return 6;
        }
        if (footworkSelected) {
            return 7;
        }
        if (protectionSelected) {
            return 8;
        }
        return 0;
    }
}